package ssau.knyazev.db;

import java.util.LinkedList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import ssau.knyazev.common.ListValue;
import ssau.knyazev.common.UniException;
import ssau.knyazev.db.common.FrameworkSettings;
import ssau.knyazev.db.interfaces.IModelManager;

public class ModelManager implements IModelManager
{
	public ListValue[] titles;
	private Document doc;
	private static final ModelManager inst = new ModelManager();
	
	private ModelManager(){
	}
	
	public static ModelManager getInst(){
		return inst;
	}
	
	@SuppressWarnings("unchecked")
	private Element findTableWithName(String tableName){
		Element root = doc.getRootElement();
		List<Element> tables = root.getChildren(FrameworkSettings.MODEL_TABLE);
		for (Element x: tables){
			if (x.getAttributeValue(FrameworkSettings.MODEL_FIELD_NAME).equals(tableName)){
				return x;
			}
		}
		return null;
	}
	
	public String getQuery(String tableName, String cmd){
		Element e = findTableWithName(tableName);
		return e.getChild(cmd).getText();
	}
	
	public Element getTableStructure(String tableName) throws UniException{
		Element table = findTableWithName(tableName);
		Element res = table.getChild("structure");
		if (res != null){
			return res;
		} else {
			throw new UniException("Did not found structure of <" + tableName + "> table ");
		}
	}
	
	public String getTableViewFrameQuery(String tableName){
		Element table = findTableWithName(tableName);
		return table.getChildText(FrameworkSettings.MODEL_TABLE_FRAME);	
	}
	
	public boolean isAutoInsertMode(String tableName){
		Element table = findTableWithName(tableName);
		return table.getAttributeValue("insert_mode") != null ? true : false;
	}
	
	
	public String getFrameDeleteQuery(String tableName){
		Element table = findTableWithName(tableName);
		return table.getChild("frame").getChild("delete").getText();
	}
	
	public String getCustomFrameName(String tableName){
		Element table = findTableWithName(tableName);
		return table.getAttributeValue("custom");
	}
	
	public String findSqlProcedure(String tableName){
		Element table = findTableWithName(tableName);
		return table.getChild("frame").getChild("procedure").getText();
	}
	
	public ListValue[] getTitles(String userRole) throws UniException{
		if (titles != null)
			return titles;
		refreshTitles(userRole);
		return titles;
	}
	
	@SuppressWarnings("unchecked")
	private void refreshTitles(String userRole) throws UniException{
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren(FrameworkSettings.MODEL_TABLE);
		List<Element> list2 = new LinkedList<Element>();
		if (list.size() == 0)
			throw new UniException("The list of tables are empty");
		for(Element x: list){
			String av = x.getAttributeValue("role");
			String[] arr = av.split("\\|");
			for (String y: arr){
				if (y.trim().equals(userRole)){
					list2.add(x);
					break;
				}
			}
		}
		titles = new ListValue[list2.size()];
		int i = 0;
		for (Element e: list2){
			String name = e.getAttributeValue("name");
			titles[i] = new ListValue(name, name);
			i++;
		}
	}

	@Override
	public List<String> getAllTitles() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> getTitlesByUserRole(String userRole) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Element findTableByName(String tableName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getQuery(Element table, int cmd) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String localizedName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
